<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="数据源名称/IP" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="数据源名称/IP"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品类型" prop="productName">
        <el-select
          v-model="queryParams.productName"
          placeholder="产品类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in productData"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb20 mt2">
      <el-col :span="1.5">
        <el-button
          type="primary"

          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :styleType.sync="styleType" :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <style-type v-if="styleType" :dataList="roleList" @handleUpdate="handleUpdate" @handleDelete="handleDelete"></style-type>
    <el-table v-if="!styleType" v-loading="loading" :data="roleList" :header-cell-style="{ background: '#F3F5FC' }" @selection-change="handleSelectionChange" height="580">
      <el-table-column label="序号" align="center" width="120">
        <template slot-scope="scop">
          {{ scop.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="数据源名称" prop="name" :show-overflow-tooltip="true" width="200"/>
      <el-table-column label="接入产品" :show-overflow-tooltip="true" width="200">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>接入产品: {{ scope.row.name }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag class="el-tag-custom" size="medium">{{ scope.row.name }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="数据库类型" prop="ip" width="140"/>
      <el-table-column label="数据库IP" align="sendIp" prop="sendIp">
      </el-table-column>
      <el-table-column label="数据库端口" align="port" prop="sendIp" width="140">
      </el-table-column>
      <el-table-column label="数据库名称" align="port" prop="sendIp" width="140">
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
                   @click="handleUpdate(scope.row)"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
                @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->
    <sys-dialog ref="jdbcRef" :title="title" :formData="formData" @changeOpen="changeOpen"/>
  </div>
</template>

<script>
import sysDialog from '@/components/accessProduct/gatherData/SysDialog'
import syslog from '../../../views/accessProduct/mockData/syslog.json'
import styleType from '@/components/accessProduct/styleType'
export default {
  name: 'Syslog',
  components:{sysDialog,styleType},
  data() {
    return {
      styleType:true,
      formData:{},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 数据范围选项
      dataScopeOptions: [
        {
          value: '1',
          label: '全部数据权限'
        },
        {
          value: '2',
          label: '自定数据权限'
        },
        {
          value: '3',
          label: '本部门数据权限'
        },
        {
          value: '4',
          label: '本部门及以下数据权限'
        },
        {
          value: '5',
          label: '仅本人数据权限'
        }
      ],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        name:'',
        productName:''
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      productData:[{
        label:'网络检测',
        value:'10'
      },{
        label:'敏感信息',
        value:'7'
      },{
        label:'刻录审计',
        value:'4'
      },{
        label:'打印审计',
        value:'3'
      },{
        label:'接入控制',
        value:'6'
      }]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading=false;
      this.roleList = syslog.list.slice(7,9)
      this.total = 2
    },
    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.menu.getCheckedKeys()
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys()
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys)
      return checkedKeys
    },
    // 所有部门节点数据
    getDeptAllCheckedKeys() {
      // 目前被选中的部门节点
      let checkedKeys = this.$refs.dept.getCheckedKeys()
      // 半选中的部门节点
      let halfCheckedKeys = this.$refs.dept.getHalfCheckedKeys()
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys)
      return checkedKeys
    },

    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([])
      }
      this.menuExpand = false,
        this.menuNodeAll = false,
        this.deptExpand = true,
        this.deptNodeAll = false,
        this.form = {
          roleId: undefined,
          roleName: undefined,
          roleKey: undefined,
          roleSort: 0,
          status: '0',
          menuIds: [],
          deptIds: [],
          menuCheckStrictly: true,
          deptCheckStrictly: true,
          remark: undefined
        }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.loading=true;
      let _self=this
      setTimeout(function(){
        _self.getList()
      },1000)
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    // 树权限（展开/折叠）
    handleCheckedTreeExpand(value, type) {
      if (type == 'menu') {
        let treeList = this.menuOptions
        for (let i = 0; i < treeList.length; i++) {
          this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value
        }
      } else if (type == 'dept') {
        let treeList = this.deptOptions
        for (let i = 0; i < treeList.length; i++) {
          this.$refs.dept.store.nodesMap[treeList[i].id].expanded = value
        }
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.$refs.jdbcRef.isOpen=true
      this.title = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.formData=row;
      this.reset()
      this.$refs.jdbcRef.isOpen=true
      this.title = '编辑'
    },
    /** 选择角色权限范围触发 */
    dataScopeSelectChange(value) {
      if (value !== '2') {
        this.$refs.dept.setCheckedKeys([])
      }
    },
    /** 分配用户操作 */
    handleAuthUser: function(row) {
      const roleId = row.roleId
      this.$router.push('/system/role-auth/user/' + roleId)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const name = row.object.name
      this.$modal.confirm('是否确认删除角色编号为"' + name + '"的数据项？').then(function() {
        return true
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/role/export', {
        ...this.queryParams
      }, `role_${new Date().getTime()}.xlsx`)
    },
    /** 改变open的值 **/
    changeOpen(value){
      this.open=value
    }
  }
}
</script>
